package com.newlink.building.common_base.ext

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.Log
import android.view.View
import android.widget.Checkable
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar
import com.newlink.building.common_base.BuildConfig
import com.newlink.building.common_base.R
import com.newlink.building.common_base.base.BaseApplication
import com.newlink.building.common_base.utils.Base_CustomImageToast
import com.newlink.building.common_base.utils.Base_CustomToast
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.ToastUtils
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

/**
 * Created by xiaodong on 2022/7/25.
 */
fun Any.loge(content: String?) {
//    loge(this.javaClass.simpleName ?: App.TAG, content ?: "")
}

fun loge(tag: String, content: String?) {
    if (BuildConfig.DEBUG) {
        Log.e(tag, content ?: "")
    }
}

fun showToast(content: String) {
    Base_CustomToast(BaseApplication.context, content).show()
}

fun Fragment.showToast(content: String) {
    Base_CustomToast(BaseApplication.context, content).show()
}

fun Fragment.showToastImmediately(content: String) {
    ToastUtils.showTextToast(BaseApplication.context, content)
}

fun Fragment.showToastLong(content: String) {
    Base_CustomToast(BaseApplication.context, content, Toast.LENGTH_LONG).show()
}

fun Fragment.showToastImmediately(@StringRes res: Int) {
    ToastUtils.showTextToast(BaseApplication.context, res)
}

fun Fragment.showSuccessToast(title: String, message: String) {
    Base_CustomImageToast(BaseApplication.context, message).show()
}

fun Context.showToast(content: String) {
    Base_CustomToast(BaseApplication.context, content).show()
}

fun Context.showToastLong(content: String) {
    Base_CustomToast(BaseApplication.context, content, Toast.LENGTH_LONG).show()
}

fun Context.showToastImmediately(content: String) {
    Base_CustomToast(BaseApplication.context, content).show()
}

fun Context.showToastImmediately(@StringRes content: Int) {
    ToastUtils.showTextToast(BaseApplication.context, content)
}

fun Context.showSuccessToast(title: String, message: String) {
    Base_CustomImageToast(BaseApplication.context, message).show()
}

fun Activity.showSnackMsg(msg: String) {
    val snackbar = Snackbar.make(this.window.decorView, msg, Snackbar.LENGTH_SHORT)
    val view = snackbar.view
    view.findViewById<TextView>(R.id.snackbar_text)
        .setTextColor(ContextCompat.getColor(this, R.color.white))
    snackbar.show()
}

fun Fragment.showSnackMsg(msg: String) {
    this.activity ?: return
    val snackbar = Snackbar.make(this.activity!!.window.decorView, msg, Snackbar.LENGTH_SHORT)
    val view = snackbar.view
    view.findViewById<TextView>(R.id.snackbar_text)
        .setTextColor(ContextCompat.getColor(this.activity!!, R.color.white))
    snackbar.show()
}

// 扩展点击事件属性(重复点击时长)
var <T : View> T.lastClickTime: Long
    set(value) = setTag(1766613352, value)
    get() = getTag(1766613352) as? Long ?: 0

// 重复点击事件绑定
inline fun <T : View> T.setSingleClickListener(time: Long = 1000, crossinline block: (T) -> Unit) {
    setOnClickListener {
        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - lastClickTime > time || this is Checkable) {
            lastClickTime = currentTimeMillis
            block(this)
        }
    }
}

/**
 * 格式化当前日期
 */
fun formatCurrentDate(): String {
    val sdf = SimpleDateFormat("yyyy-MM-dd")
    return sdf.format(Date())
}

/**
 * String 转 Calendar
 */
fun String.stringToCalendar(): Calendar {
    val sdf = SimpleDateFormat("yyyy-MM-dd")
    val date = sdf.parse(this)
    val calendar = Calendar.getInstance()
    calendar.time = date
    return calendar
}


private var clickEffectiveTime = 1500L
private var lastClickTime1: Long = 0
private var hash: Int = 0

infix fun View.clickDelay(clickAction: () -> Unit) {
    this.setOnClickListener {
        if (this.hashCode() != hash) {
            hash = this.hashCode()
            lastClickTime1 = System.currentTimeMillis()
            clickAction()
        } else {
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastClickTime1 > clickEffectiveTime) {
                lastClickTime1 = System.currentTimeMillis()
                clickAction()
            }
        }
    }
}

fun Int.toPaddedHexString(length: Int = 4): String {
    return this.toString(16).padStart(length, '0')
}

fun Short.toPaddedHexString(length: Int = 4): String {
    return this.toString(16).padStart(length, '0')
}

fun Bitmap.drawRect(block: (canvas: Canvas, paint: Paint) -> Unit): Bitmap {
    var result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    try {
        val canvas = Canvas(result)
        canvas.drawBitmap(this, 0f, 0f, null)
        val paint = Paint()
        paint.strokeWidth = 6f
        paint.style = Paint.Style.STROKE
        paint.color = Color.RED

        block(canvas, paint)

        canvas.save()
        canvas.restore()
    } catch (e: Exception) {
        NLog.e("${e.message}")
    }
    return result
}